package aes.gui;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import aes.general.Exam;
import aes.general.Teacher;
import aes.general.TeacherControl;

/**
 * The Class TeacherExamPanel. This panel is allowing the teacher to compose an exam.
 * Add questions, to the exam, assign points and set the exam duration.
 * When the exam creation process is complete.
 * The system stores it to the Data Base.
 */
public class TeacherExamPanel extends JPanel {

	/**
	 *  
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JPanel Expanel;
	private JPanel Comments;
	private JPanel QuestionComboPanel;
	private JPanel SubjectCoursePanel;
	private JPanel LastButtons;
	private JPanel ExamNumberDurationPanel;
	private JPanel QuestionText;
	private JPanel FirstButtons;
	private JPanel QuestionAdded;
	private JPanel totalpoints;
	private JLabel l1;
	private JLabel l2;
	private JLabel l3;
	private JLabel l4;
	private JLabel l5;
	private JLabel Points;
	private JLabel TotalPoints;
	private JLabel label;
	private JLabel lblSetExamDuration;
	private JButton b1;
	private JButton b2;
	private JButton b3;
	private JButton b4;
	private JButton b5;
	private JComboBox cb;
	private JComboBox cb1;
	private JComboBox cb2;
	private JComboBox cb3;
	private JTextField t1;
	private JTextField t2;
	private JTextField t3;
	private JTextField t4;
	private JTextField t5;
	private JTextField txtduration;
	private JTextArea txt;
	private JSeparator separator;

	private Exam exam = new Exam();
	int flag = 1;
	int i = 0;
	private ArrayList<String> courses = new ArrayList<String>();
	private ArrayList<String> CourseName = new ArrayList<String>();
	private ArrayList<String> CourseId = new ArrayList<String>();
	private ArrayList<String> Question = new ArrayList<String>();
	private ArrayList<String> QuestionId = new ArrayList<String>();

	/**
	 * Create the panel.
	 * 
	 * @param contentPane
	 *            the content pane
	 */
	public TeacherExamPanel(JPanel contentPane) {
		this.jp = contentPane;
		initialize();
	}

	/**
	 * Initialize.
	 */
	@SuppressWarnings("unchecked")
	private void initialize() {
		setLayout(null);
		
		Comments = new JPanel();
		Comments.setBounds(10, 0, 48, 40);
		add(Comments);
		Comments.setLayout(null);
		
		SubjectCoursePanel = new JPanel();
		SubjectCoursePanel.setBounds(10, 51, 227, 73);
		add(SubjectCoursePanel);
		SubjectCoursePanel.setLayout(null);
				
		l5 = new JLabel("Choose Course:");
		l5.setBounds(10, 45, 106, 14);
		SubjectCoursePanel.add(l5);
		l5.setHorizontalAlignment(SwingConstants.LEFT);
		
		l4 = new JLabel("Choose Subject:");
		l4.setBounds(11, 14, 105, 14);
		SubjectCoursePanel.add(l4);

		cb1 = new JComboBox();
		cb1.setBounds(126, 11, 86, 20);
		SubjectCoursePanel.add(cb1);
		
		cb2 = new JComboBox();
		cb2.setBounds(126, 42, 86, 20);
		SubjectCoursePanel.add(cb2);
		cb2.addItem("-------");
		cb2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (((String) cb2.getSelectedItem()) != "-------" && ((String) cb1.getSelectedItem()) != "-------" && (String) cb2.getSelectedItem() != null) {
										t2.setText(CourseId.get(CourseName.indexOf((String)cb2.getSelectedItem())));
				} else
					t2.setText(null);
			}
			});

		cb1.addItem("-------");
		cb1.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent e) {
				if (((String) cb1.getSelectedItem()) != "-------") {
					t1.setText(Teacher.getSid().get(Teacher.getSname().indexOf((String) cb1.getSelectedItem())));
					courses.add(Teacher.getSid().get(Teacher.getSname().indexOf((String) cb1.getSelectedItem())));
					
					TeacherControl.getCourses(courses);
               
	                try {
	                        Thread.sleep(300);
	                } catch (InterruptedException e1) {
	                        e1.printStackTrace();
	                }
	                
	                TeacherControl.getQid(courses);
	                
	                try {
                        Thread.sleep(300);
	                } catch (InterruptedException e1) {
	                        e1.printStackTrace();
	                }
	                
	                cb2.removeAllItems();
	                cb2.addItem("-------");
	                CourseId = TeacherControl.getCurseId();
	                CourseName = TeacherControl.getCourseName();
	                
	                for(int i=0;i<CourseName.size();i++)
	                	cb2.addItem(CourseName.get(i));
	                courses.clear();
						
	                cb.removeAllItems();
	                cb.addItem("-------");
	                QuestionId = TeacherControl.getQid();
	                
	                for(int i=0;i<QuestionId.size();i++)
	                	cb.addItem(QuestionId.get(i));
					} 
				else {
					t1.setText(null);
					t2.setText("-------");
				}
			}
		});
		
		ExamNumberDurationPanel = new JPanel();
		ExamNumberDurationPanel.setBounds(10, 199, 223, 140);
		add(ExamNumberDurationPanel);
		ExamNumberDurationPanel.setLayout(null);
		ExamNumberDurationPanel.setVisible(false);
		
		lblSetExamDuration = new JLabel("Set Exam Duration In Minutes:");
		lblSetExamDuration.setBounds(10, 11, 187, 14);
		ExamNumberDurationPanel.add(lblSetExamDuration);
		
		txtduration = new JTextField();
		txtduration.setBounds(49, 35, 61, 20);
		
		ExamNumberDurationPanel.add(txtduration);
		txtduration.setFont(new Font("Tahoma", Font.BOLD, 14));
		txtduration.setColumns(6);
		t1 = new JTextField();
		t1.setBounds(38, 96, 26, 20);
		ExamNumberDurationPanel.add(t1);
		t1.setFont(new Font("Tahoma", Font.BOLD, 14));
		t1.setEditable(false);
		t1.setColumns(6);
								
		t2 = new JTextField();
		t2.setBounds(74, 96, 26, 20);
		ExamNumberDurationPanel.add(t2);
		t2.setFont(new Font("Tahoma", Font.BOLD, 14));
		t2.setEditable(false);
		t2.setColumns(6);
		
		t3 = new JTextField();
		t3.setBounds(107, 96, 42, 20);
		ExamNumberDurationPanel.add(t3);
		t3.setFont(new Font("Tahoma", Font.BOLD, 14));
		t3.setColumns(6);
		
		l2 = new JLabel("Set Exam Number : ");
		l2.setBounds(10, 71, 161, 14);
		ExamNumberDurationPanel.add(l2);
		
		QuestionComboPanel = new JPanel();
		QuestionComboPanel.setBounds(10, 51, 227, 134);
		add(QuestionComboPanel);
		QuestionComboPanel.setLayout(null);
		QuestionComboPanel.setVisible(false);
		
		b3 = new JButton("Add Question");
		b3.setBounds(42, 98, 117, 23);
		QuestionComboPanel.add(b3);
		b3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (cb.getSelectedIndex() != 0 && t4.getText().trim().length() != 0) {
					int num1 = Integer.valueOf(t4.getText());
					int num2 = Integer.valueOf(t5.getText());
					int total = num1 + num2;
					if (total <= 100) 
					{
						String qust = TeacherControl.getQid().get(TeacherControl.getQid().indexOf((String) cb.getSelectedItem()));

						t5.setText(null);
						t5.setText(Integer.toString(total));

						// input: question id
						exam.setQuestions(qust);
						// input: number of points
						exam.setQuestionPoints(t4.getText());
						cb.removeItem(qust);
						
						Question = exam.getQuestions();
					
						cb3.removeAllItems();
						cb3.addItem("-------");
						for(i=0;i<Question.size();i++)
							cb3.addItem(Question.get(i));
					} 
					else
						JOptionPane.showMessageDialog(null,
								"Error,Number of Points Exceeds 100!", "Error",
								JOptionPane.ERROR_MESSAGE);
	
					} 
					else if (cb.getSelectedIndex() == 0)
						JOptionPane.showMessageDialog(null,
								"Error,No Question Selected!", "Error",
								JOptionPane.ERROR_MESSAGE);
	
					else if (t4.getText().trim().length() == 0)
						JOptionPane.showMessageDialog(null,
								"Error,No Points Selected!", "Error",
								JOptionPane.ERROR_MESSAGE);
			}
		});



		t4 = new JTextField();
		t4.setEditable(false);
		t4.setBounds(78, 67, 42, 20);
		QuestionComboPanel.add(t4);
		t4.setFont(new Font("Tahoma", Font.BOLD, 14));
		t4.setColumns(6);
		
		Points = new JLabel("Set Number Of Points:");
		Points.setBounds(49, 42, 134, 14);
		QuestionComboPanel.add(Points);
		
		label = new JLabel("Choose Questions:");
		label.setBounds(10, 14, 117, 14);
		QuestionComboPanel.add(label);
		
		cb = new JComboBox();
		cb.setBounds(125, 11, 86, 20);
		QuestionComboPanel.add(cb);
		cb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			if(cb.getSelectedItem() != "-------" && cb.getSelectedItem() != null){
				
				TeacherControl.getQuestion((String) cb.getSelectedItem());
				 try {
                     Thread.sleep(100);
	                } catch (InterruptedException e1) {
	                        e1.printStackTrace();
	                }
				 txt.setText("Question:\n" + TeacherControl.getQuestion() + "\n\nDescription:\n" + TeacherControl.getDescription() +
						 "\n\nAnswers:\n1. " + TeacherControl.getAns1() + "\n2. " + TeacherControl.getAns2() + 
						 "\n3. " + TeacherControl.getAns3() + "\n4. " + TeacherControl.getAns4());
				 t4.setEditable(true);
			}
			else
			{
				t4.setEditable(false);
				t4.setText(null);
			}
				
			}
		});
		
		QuestionText = new JPanel();
		QuestionText.setBounds(227, 86, 276, 304);
		add(QuestionText);
		QuestionText.setLayout(null);
		setLayout(null);
		QuestionText.setVisible(false);

		l1 = new JLabel("Compose Exam");
		l1.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
		l1.setBounds(235, 11, 169, 29);
		add(l1);

		separator = new JSeparator();
		separator.setBounds(10, 43, 620, 2);
		add(separator);
		if(flag == 1){
			for (String subj : Teacher.getSname())
			{
				if(cb1.equals(subj) == false)
					cb1.addItem(subj);
			}
			flag = 0;
		}
		
		Expanel = new JPanel();
		jp.add(Expanel, "main");
		Comments = new TeacherCommentPanel(jp, exam);
		jp.add(Comments, "comments");

		txt = new JTextArea();
		txt.setBounds(10, 11, 259, 282);
		QuestionText.add(txt);
		txt.setEditable(false);
						
		FirstButtons = new JPanel();
		FirstButtons.setBounds(256, 400, 97, 34);
		add(FirstButtons);
		FirstButtons.setLayout(null);
		
		LastButtons = new JPanel();
		LastButtons.setBounds(113, 388, 242, 40);
		add(LastButtons);
		LastButtons.setLayout(null);
		LastButtons.setVisible(false);
		
		b5 = new JButton("Next");
		b5.setBounds(0, 0, 91, 23);
		FirstButtons.add(b5);
		b5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (((String) cb2.getSelectedItem()) != "-------" && ((String) cb1.getSelectedItem()) != "-------" && (String) cb2.getSelectedItem() != null) {
					ExamNumberDurationPanel.setVisible(true);
					QuestionComboPanel.setVisible(true);
					SubjectCoursePanel.setVisible(false);
					LastButtons.setVisible(true);
					FirstButtons.setVisible(false);
					QuestionAdded.setVisible(true);
					QuestionText.setVisible(true);
					totalpoints.setVisible(true);
				}
				else
					JOptionPane.showMessageDialog(null,
							"Error,Must Choose Subject and Course!", "Error",
							JOptionPane.ERROR_MESSAGE);
			}
		});
										
		b4 = new JButton("Add Comments");
		b4.setBounds(0, 11, 127, 23);
		LastButtons.add(b4);
		b4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl = (CardLayout) (jp.getLayout());
				cl.show(jp, "comments");
			}
		});
		
		b1 = new JButton("Save Exam");
		b1.setBounds(133, 11, 105, 23);
		LastButtons.add(b1);
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (t3.getText().trim().length() == 0)
					JOptionPane.showMessageDialog(null,"Error,Can't leave the Exam number blank!","Error", JOptionPane.ERROR_MESSAGE);
				else if (t3.getText().trim().length() != 2) {
					JOptionPane.showMessageDialog(null,
							"Error,Number Must Be 2 Digits!", "Error",
							JOptionPane.ERROR_MESSAGE);
				} else if (exam.getQuestions().isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Error,No Questions Added to The Exam!", "Error",
							JOptionPane.ERROR_MESSAGE);
				} else if (txtduration.getText().trim().length() == 0) {
					JOptionPane.showMessageDialog(null,
							"Error,No Exam Duration Selected!", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
				
				else {
					// Exam id
					exam.setExamId(t1.getText()+t2.getText()+t3.getText());
					// Exam Subject
					exam.setExamSubject(t1.getText());
					// Exam Duration
					
					exam.setDuration(Integer.decode(txtduration.getText()));
					// Exam Composer
					exam.setTeacherId(Teacher.getTeacherID());
					
					TeacherControl.AddExam(exam);

					try {
						Thread.sleep(600);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					/**
					 * Checking if the exam id is unique
					 */
					 if(TeacherControl.ExamIdError() == false)
					 	JOptionPane.showMessageDialog(null,"Error,Exam With This Number Already Exists!", "Error",
					 				JOptionPane.ERROR_MESSAGE);
					 else	
					 {
					 	JOptionPane.showMessageDialog(null,"Exam Has Been Saved!", "OK!",
					 				JOptionPane.INFORMATION_MESSAGE);
					 
						 exam.setExamId(null);
					 	 exam.setDescription(null);
						 exam.setVisible(false);
						 exam.EmptyQuestions();
						 exam.EmptyQuestionPoints();
						 exam.setDescription(null);
						 cb.setSelectedIndex(0);
						 cb1.setSelectedIndex(0);
						 cb2.setSelectedIndex(0);
						 cb3.removeAllItems();
						 cb3.addItem("-------");
						 t3.setText(null);
						 t4.setText(null);
						 t5.setText("0");
						 txtduration.setText(null);
						 txt.setText(null);
						 flag = 1;
						 
						 ExamNumberDurationPanel.setVisible(false);
						 QuestionComboPanel.setVisible(false);
						 SubjectCoursePanel.setVisible(true);
						 LastButtons.setVisible(false);
						 FirstButtons.setVisible(true);
						 QuestionAdded.setVisible(false);
						 QuestionText.setVisible(false);
						 totalpoints.setVisible(false);
							
						 CardLayout cl = (CardLayout) (jp.getLayout());
						 cl.show(jp, "mainPanel");
					 }	
				}
			}
		});
		
		b2 = new JButton("Cancel");
		b2.setBounds(358, 400, 105, 23);
		add(b2);
		
		QuestionAdded = new JPanel();
		QuestionAdded.setBounds(506, 61, 105, 96);
		add(QuestionAdded);
		QuestionAdded.setLayout(null);
		QuestionAdded.setVisible(false);
		
		cb3 = new JComboBox();
		cb3.setBounds(0, 25, 80, 20);
		QuestionAdded.add(cb3);
		cb3.addItem("-------");
		
		l3 = new JLabel("Question Added:");
		l3.setBounds(0, 0, 105, 14);
		QuestionAdded.add(l3);
		
		totalpoints = new JPanel();
		totalpoints.setBounds(240, 47, 256, 37);
		add(totalpoints);
		totalpoints.setVisible(false);
		totalpoints.setLayout(null);
		
		TotalPoints = new JLabel("Total Number Of Points:");
		TotalPoints.setBounds(10, 11, 144, 14);
		totalpoints.add(TotalPoints);
						
		t5 = new JTextField();
		t5.setBounds(146, 5, 84, 23);
		totalpoints.add(t5);
		t5.setEditable(false);
		t5.setFont(new Font("Tahoma", Font.BOLD, 14));
		t5.setColumns(6);
		t5.setText("0");
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exam.setDescription(null);
				exam.setVisible(false);
				exam.EmptyQuestions();
				exam.EmptyQuestionPoints();
				exam.setDescription(null);
				cb.setSelectedItem("-------");
				cb1.setSelectedItem("-------");
				cb2.setSelectedItem("-------");
				cb3.removeAllItems();
				cb.removeAllItems();
				cb3.addItem("-------");
				cb.addItem("-------");
				t3.setText(null);
				t4.setText(null);
				t5.setText("0");
				txtduration.setText(null);
				flag = 1;
				
				ExamNumberDurationPanel.setVisible(false);
				QuestionComboPanel.setVisible(false);
				SubjectCoursePanel.setVisible(true);
				LastButtons.setVisible(false);
				FirstButtons.setVisible(true);
				QuestionAdded.setVisible(false);
				QuestionText.setVisible(false);
				totalpoints.setVisible(false);
				
				CardLayout cl = (CardLayout) (jp.getLayout());
				cl.show(jp, "mainPanel");
			}
		});
	}
}
